package com.antler.smt.portal.service.impl;

import com.antler.smt.constant.OrderCommentConstant;
import com.antler.smt.constant.OrderConstant;
import com.antler.smt.entity.*;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.portal.mapper.*;
import com.antler.smt.request.portal.OrderCommentReq;
import com.antler.smt.response.base.Result;
import com.antler.smt.response.portal.GetOrderCommentListResp;
import com.antler.smt.service.portal.OrderCommentService;
import com.antler.smt.utils.DateUtil;
import net.bytebuddy.asm.Advice;
import org.apache.ibatis.builder.ResultMapResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 订单评论实现
 *
 * @author zhangyi
 * @date 2018/4/13 9:44
 */
@Service
public class OrderCommentServiceImpl implements OrderCommentService {

    @Autowired
    private OrderCommentEntityMapper orderCommentEntityMapper;

    @Autowired
    private OrderEntityMapper orderEntityMapper;

    @Autowired
    private OrderCommentInfoMapper orderCommentInfoMapper;

    @Autowired
    private RequirementEntityMapper requirementEntityMapper;

    @Autowired
    private PurchaserEntityMapper purchaserEntityMapper;

    @Autowired
    private MemberEntityMapper memberEntityMapper;

    @Autowired
    private OrderCommentPictureEntityMapper orderCommentPictureEntityMapper;

    @Autowired
    private AttachmentEntityMapper attachmentEntityMapper;

    @Autowired
    private OrderCommentPictureInfoMapper orderCommentPictureInfoMapper;
    /**
     * 插入订单评价
     *
     * @param orderId
     * @param req
     * @return
     */
    @Override
    public Result<ResultCodeEnum> insertOrderComment(Long orderId, OrderCommentReq req,List<Long> attachmentIds) {

        Result result = null;

        //订单评价实体
        OrderCommentEntity orderCommentEntity = new OrderCommentEntity();

        //订单实体
        OrderEntity orderEntity = new OrderEntity();

        //添加订单号
        orderEntity.setId(orderId);

        //修改订单状态
        orderEntity.setStatus(OrderConstant.ORDER_WAIT_PAY_SUPPLIER);

        int j = orderEntityMapper.updateByPrimaryKeySelective(orderEntity);

        //添加订单号
        orderCommentEntity.setOrderId(orderId);

        //添加评论平台内容
        orderCommentEntity.setToPlatform(req.getToPlatform());

        //添加评论服务商的内容
        orderCommentEntity.setToSupplier(req.getToSupplier());

        //添加评论星级
        orderCommentEntity.setGrade(req.getGrade());

        //添加使用时间
        orderCommentEntity.setUseTime(DateUtil.stampToDate(System.currentTimeMillis()));

        //添加状态 1有效，2作废(默认不展示)
        orderCommentEntity.setStatus(OrderCommentConstant.ORDER_COMMENT_NO);

        //添加创建时间
        orderCommentEntity.setCreateTime(DateUtil.stampToDate(System.currentTimeMillis()));
        orderCommentEntity.setUpdateTime(DateUtil.stampToDate(System.currentTimeMillis()));

        int i = orderCommentEntityMapper.insertSelective(orderCommentEntity);
        Long primaryKey = orderCommentInfoMapper.getPrimaryKey();

        System.out.println("------------------------"+primaryKey);
        OrderCommentPictureEntity orderCommentPictureEntity = null;

        if(attachmentIds != null) {
            //批量插入
            for (Long key : attachmentIds) {
                orderCommentPictureEntity = new OrderCommentPictureEntity();
                orderCommentPictureEntity.setAttactmentId(key);
                orderCommentPictureEntity.setCommnetId(primaryKey);
                orderCommentPictureEntity.setCreateTime(DateUtil.stampToDate(System.currentTimeMillis()));
                orderCommentPictureEntity.setUpdateTime(DateUtil.stampToDate(System.currentTimeMillis()));
                orderCommentPictureEntityMapper.insertSelective(orderCommentPictureEntity);
            }
        }

        if((i & j) == 0){
            result = new Result(ResultCodeEnum.APPEND_DATA_ERROR);
        }else{
            result = new Result(ResultCodeEnum.OPERATION_SUCCESS);
        }
        return result;
    }

    /**
     *  首页展示优质订单
     *
     * @return
     */
    @Override
    public Result getOrderCommentList() {

        Result result = null;
        //获取所有有效的订单列表
        List<GetOrderCommentListResp> orderCommentList = orderCommentInfoMapper.getOrderCommentList();

        for(int i = 0; i < orderCommentList.size(); i++) {
            OrderEntity orderEntity = orderEntityMapper.selectByPrimaryKey(orderCommentList.get(i).getOrderId());
            RequirementEntity requirementEntity = requirementEntityMapper.selectByPrimaryKey(orderEntity.getRequirementId());
            PurchaserEntity purchaserEntity = purchaserEntityMapper.selectByPrimaryKey(requirementEntity.getPurchaserId());
            MemberEntity memberEntity = memberEntityMapper.selectByPrimaryKey(purchaserEntity.getMemberId());

            if(memberEntity != null) {
                //头像id
                Long headImageAttachmentId = memberEntity.getHeadImageAttachmentId();
                AttachmentEntity attachmentEntity = attachmentEntityMapper.selectByPrimaryKey(headImageAttachmentId);
                if(attachmentEntity != null) {
                    orderCommentList.get(i).setMemberImg(attachmentEntity.getFileName());
                }
            }
            List<String> commentList = orderCommentPictureInfoMapper.getCommentList(orderCommentList.get(i).getId());
            orderCommentList.get(i).setCommentImgs(commentList);
        }

        if(orderCommentList.size() == 0){
            result  = new Result(ResultCodeEnum.OPERATION_SUCCESS,"当前无评价");
        }else {
            //需要展示的订单评价列表
            List<Object> orderCommentListShow = new ArrayList<>();

            if(orderCommentList.size() <= OrderCommentConstant.ORDER_COMMENT_AMOUNT){
                orderCommentListShow.addAll(orderCommentList);
            }else{
                //选择 需要展示的个数
                for (int i = 0; i < OrderCommentConstant.ORDER_COMMENT_AMOUNT; i++) {
                    GetOrderCommentListResp orderComment = orderCommentList.get(i);
                    //订单id
                    Long orderId = orderComment.getOrderId();
                    //订单实体
                    OrderEntity orderEntity = orderEntityMapper.selectByPrimaryKey(orderId);
                    //发布需求id
                    Long requirementId = orderEntity.getRequirementId();

                    RequirementEntity requirementEntity =
                            requirementEntityMapper.selectByPrimaryKey(requirementId);
                    //采购商id
                    Long purchaserId = requirementEntity.getPurchaserId();

                    //采购商实体
                    PurchaserEntity purchaserEntity =
                            purchaserEntityMapper.selectByPrimaryKey(purchaserId);

                    //采购商会员实体
                    MemberEntity memberEntity =
                            memberEntityMapper.selectByPrimaryKey(purchaserEntity.getMemberId());

                    orderComment.setName(requirementEntity.getName());

                    orderComment.setNickName(memberEntity.getNickName());
                    orderComment.setEmail(memberEntity.getEmail());
                    orderComment.setMoblie(memberEntity.getMobile());

                    orderCommentListShow.add(orderComment);

                }
            }
            result = new Result(orderCommentListShow);
        }
        return result;
    }

    /**
     *  获取当前订单评价
     *
     * @param orderId 订单id
     * @return Result<GetOrderCommentListResp>
     */
    @Override
    public Result<GetOrderCommentListResp> getOrderInfoByOrderIdShowPurchaser(Long orderId) {
        Result result = null;

        GetOrderCommentListResp orderInfo = orderCommentInfoMapper.getOrderInfoByOrderIdShowPurchaser(orderId);

        //获取订单实体
        OrderEntity orderEntity = orderEntityMapper.selectByPrimaryKey(orderId);

        RequirementEntity requirementEntity = requirementEntityMapper.selectByPrimaryKey(orderEntity.getRequirementId());

        PurchaserEntity purchaserEntity = purchaserEntityMapper.selectByPrimaryKey(requirementEntity.getPurchaserId());

        MemberEntity memberEntity = memberEntityMapper.selectByPrimaryKey(purchaserEntity.getMemberId());

        List list = new LinkedList();
        if(orderInfo != null){

            //有评价
            list.add(orderInfo);
            list.add(memberEntity);
            list.add(requirementEntity.getName());

            result = new Result(list);
        }else{
            //无评价
            result = new Result(null);
        }
        return result;
    }
}
